# -*- coding: utf-8 -*-
from sqlalchemy import Column,String,Integer,Text,Binary,Boolean,DateTime,Enum,Float
from adminApp.extensions import db
from .base import BaseModel


class Machine(BaseModel, db.Model):
    '''机器设备'''
    system_uuid = Column(String(64), unique=True)   # 系统ID
    aqy_device_id = Column(String(64))              # 爱奇艺ID
    system_type = Column(String(64))                # 系统类型 Centos debian
    system_product_name = Column(String(64))        # 机器类型名称
    kernel_version = Column(String(64))             # 内核版本
    hostname = Column(String(64))                   # 主机名称
    is_virtual = Column(Boolean)                    # 是否虚拟机
    is_pve = Column(Boolean)                        # 是否PVE
    is_centos = Column(Boolean)                     # 是否Centos
    # 爱奇艺ID 认证状态
    aqy_cert_status = Column(Boolean, default=False)
    # 爱奇艺ID 认证日期
    aqy_cert_date = Column(DateTime)
    # 最后一次接收心跳包时间 第二版弃用
    heart_last_receive = Column(DateTime)
    # 显示速率的默认网卡名称
    default_nic = Column(String(64))
    # 默认网卡实时接收流量 单位字节/秒
    default_nic_rx = Column(Float)
    # 默认网卡实时发送流量 单位字节/秒
    default_nic_tx = Column(Float)
    # 网络接口信息
    netfaces = Column(Text)
    # 路由信息
    gateways = Column(Text)
    # 内置路由拨号信息
    virsh_accounts = Column(Text)
    # 爱奇艺8400端口IP统计
    exportstatus_8400 = Column(Text)
    # 链路IP数量
    ip_num = Column(Integer)
    # 客户端程序版本
    client_version = Column(String(32))
    # 是否在线
    is_line =  Column(Boolean, default=False)
    # 是否触发报警
    is_trigger_warn = Column(Boolean, default=True)
    # 是否自动更新客户端程序
    is_auto_update_program = Column(Boolean, default=True)
    # 备注
    remark = Column(Text)
    # 自定义标识
    man_mark = Column(String(128))
    # 设备确认状态
    man_confirm = Column(Boolean, default=False)
    # 设备8400 端口 telnet tcp 测试
    port_8400_chk = Column(String(128))
    # 设备8400 端口 telnet tcp 上次测试时间
    port_8400_chk_last = Column(DateTime)
    # 8400 及服务端口检测结果
    port_chk_result = Column(Text)
    # 8400 及服务端口检测失败数目
    port_chk_err_num = Column(Integer)
    # 虚拟机的宿主机 PVE 的记录ID
    host_pve_machine_id = Column(Integer)
    # 是否历史记录
    is_history_log = Column(Boolean, default=False)
    # 默认接口NatType,默认接口外网IP，配置带宽，多线类型，onlykcp,备注
    def_nic_nat_type = Column(String(128))
    def_nic_wan_ip = Column(String(128))
    aqy_band_width = Column(Integer)
    multi_line_scene = Column(Integer)
    only_kcp = Column(Integer)
    aqy_xml_conf= Column(Text)
    aqy_json_conf = Column(Text)
    dcache_hardware_info = Column(Text)
    # 机房ID
    idc_id = Column(Integer, db.ForeignKey('idc.id'), index=True)
    idc = db.relationship("Idc", back_populates="machines")
    # 网卡
    network_cards = db.relationship('NetWorkCard', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    # 网卡
    defaultNicFlowDatas = db.relationship('DefaultNicFlowData', back_populates='machine', lazy='dynamic',
                                          cascade="all, delete-orphan")
    # 硬盘
    hard_disks = db.relationship('HardDisk', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    # 任务记录
    task_logs = db.relationship('TaskLog', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    # 警报记录
    warn_logs = db.relationship('WarnLogs', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    bandwidthninefivelogs = db.relationship('BandWidthNineFiveLog', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    bandwidthlogs = db.relationship('BandWidthLog', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    bandwidthhistorypics = db.relationship('BandWidthHistoryPic', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    pvevmconfes = db.relationship('PveVmConf', back_populates='_machine', lazy='dynamic', cascade="all, delete-orphan")
    aqyBandWidth95s = db.relationship('AqyBandWidth95', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")

    source_aqy_snmp_logs = db.relationship('SourceAqySnmpLog', back_populates='machine', lazy='dynamic', cascade="all, delete-orphan")
    source_my_snmp_logs = db.relationship('SourceMySnmpLog', back_populates='machine', lazy='dynamic',
                                           cascade="all, delete-orphan")

    def __str__(self):
        return '<{},{},{},{}>'.format(self.id, self.idc.name if self.idc else None, self.hostname, self.man_mark)